package cn.demographic.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.demographic.dao.LiveMapper;
import cn.demographic.dao.RegistryMapper;
import cn.demographic.entity.Live;
import cn.demographic.entity.Registry;
import cn.demographic.service.RegistryService;
import cn.demographic.util.MD5;
@Service
public class RegistryServiceImpl implements RegistryService {
	@Resource
	private RegistryMapper registryMapper;
	@Resource
	private LiveMapper liveMapper;
	
	
	@Override
	public Registry getLogin(String telephone, String pwd) {
		String pass=MD5.getMd5(pwd, 32);
		Registry user = registryMapper.getLoginByUserCode(telephone);
		if (user != null && !(user.getPassword().equals(pass))) {
			user = null;
		}
		return user;
	}
	
	@Transactional
	@Override
	public int addregistry(String name, String phone, String pwd, String unit, Integer pid,
							String pcode, String scode, String xiancode) {
		Live live = new Live();
		live.setCity(scode);
		live.setCounty(xiancode);
		live.setProvince(pcode);
		liveMapper.addLivePersonal(live);
		Registry registry = new Registry();
		registry.setName(name);
		String pass=MD5.getMd5(pwd, 32);
		registry.setPassword(pass);
		registry.setPhone(phone);
		registry.setState(0);
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.format(date);
		registry.setEntertime(date);
		registry.setUnit(unit);
		registry.setLiveId(live.getId());
		registry.setPermissionsId(pid);
		return registryMapper.addregistry(registry);
	}
	
	/**
	 * 验证手机号是不是已经被注册
	 * 
	 */
	@Override
	public boolean gettelephone(String telephone) { 
		boolean b = false;
		Registry phone	=registryMapper.gettelephone(telephone);
		if(phone == null) {
			b = true;
		}
		return b;
	}
	
	
	
	
	/**
	 * 查询注册信息审核状态
	 * @param permissions_id
	 * @param state
	 * @return
	 */
	public List<Registry> findRegistryAudit(Map<String,Object> map){
		return registryMapper.findRegistryAudit(map);
	};
	
	/**
	 * 查询注册信息审核状态的数量
	 * @param map
	 * @return
	 */
	public Long findCountRegistryAudit(Map<String,Object> map) {
		return registryMapper.findCountRegistryAudit(map);
	};
	
	/**
	 * 根据id修改注册的审核状态
	 * @param id
	 * @return
	 */
	public Integer UpdateRegistryAudit(@Param("id")Integer[] id, @Param("state")Integer state ) {
		Integer count =  registryMapper.UpdateRegistryAudit(id, state);
		return count;
	};
}
